﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CoffeeChat.Client
{
    public class ChatChannel : Form
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if ( disposing && ( components != null ) )
            {
                components.Dispose();
            }
            base.Dispose( disposing );
        }
        private void InitializeComponent()
        {
            this.textBoxLog = new System.Windows.Forms.RichTextBox();
            this.textBoxMsg = new System.Windows.Forms.TextBox();
            this.panelChatBox = new System.Windows.Forms.Panel();
            this.toolStripCustomChat = new System.Windows.Forms.ToolStrip();
            this.menuEmoticons = new System.Windows.Forms.ToolStripDropDownButton();
            this.menuItemEmoticonHappy = new System.Windows.Forms.ToolStripMenuItem();
            this.menuItemEmoticonSad = new System.Windows.Forms.ToolStripMenuItem();
            this.buttonChangeChannel = new System.Windows.Forms.Button();
            this.toolStripMenu = new System.Windows.Forms.ToolStrip();
            this.menuItemChangeChannel = new System.Windows.Forms.ToolStripButton();
            this.panelChatBox.SuspendLayout();
            this.toolStripCustomChat.SuspendLayout();
            this.toolStripMenu.SuspendLayout();
            this.SuspendLayout();
            // 
            // textBoxLog
            // 
            this.textBoxLog.BackColor = System.Drawing.Color.White;
            this.textBoxLog.DetectUrls = false;
            this.textBoxLog.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBoxLog.HideSelection = false;
            this.textBoxLog.Location = new System.Drawing.Point(0, 25);
            this.textBoxLog.Name = "textBoxLog";
            this.textBoxLog.ReadOnly = true;
            this.textBoxLog.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
            this.textBoxLog.Size = new System.Drawing.Size(296, 190);
            this.textBoxLog.TabIndex = 3;
            this.textBoxLog.Text = "";
            // 
            // textBoxMsg
            // 
            this.textBoxMsg.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBoxMsg.Location = new System.Drawing.Point(0, 25);
            this.textBoxMsg.MaxLength = 0;
            this.textBoxMsg.Name = "textBoxMsg";
            this.textBoxMsg.Size = new System.Drawing.Size(296, 20);
            this.textBoxMsg.TabIndex = 1;
            // 
            // panelChatBox
            // 
            this.panelChatBox.Controls.Add(this.textBoxMsg);
            this.panelChatBox.Controls.Add(this.toolStripCustomChat);
            this.panelChatBox.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelChatBox.Location = new System.Drawing.Point(0, 215);
            this.panelChatBox.Name = "panelChatBox";
            this.panelChatBox.Size = new System.Drawing.Size(296, 47);
            this.panelChatBox.TabIndex = 2;
            // 
            // toolStripCustomChat
            // 
            this.toolStripCustomChat.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.toolStripCustomChat.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuEmoticons});
            this.toolStripCustomChat.Location = new System.Drawing.Point(0, 0);
            this.toolStripCustomChat.Name = "toolStripCustomChat";
            this.toolStripCustomChat.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;
            this.toolStripCustomChat.Size = new System.Drawing.Size(296, 25);
            this.toolStripCustomChat.TabIndex = 5;
            // 
            // menuEmoticons
            // 
            this.menuEmoticons.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.menuEmoticons.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuItemEmoticonHappy,
            this.menuItemEmoticonSad});
            this.menuEmoticons.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.menuEmoticons.Name = "menuEmoticons";
            this.menuEmoticons.Size = new System.Drawing.Size(68, 22);
            this.menuEmoticons.Text = "Emoticons";
            // 
            // menuItemEmoticonHappy
            // 
            this.menuItemEmoticonHappy.Name = "menuItemEmoticonHappy";
            this.menuItemEmoticonHappy.Size = new System.Drawing.Size(152, 22);
            this.menuItemEmoticonHappy.Text = "Happy";
            // 
            // menuItemEmoticonSad
            // 
            this.menuItemEmoticonSad.Name = "menuItemEmoticonSad";
            this.menuItemEmoticonSad.Size = new System.Drawing.Size(152, 22);
            this.menuItemEmoticonSad.Text = "Sad";
            // 
            // buttonChangeChannel
            // 
            this.buttonChangeChannel.Location = new System.Drawing.Point(0, 0);
            this.buttonChangeChannel.Name = "buttonChangeChannel";
            this.buttonChangeChannel.Size = new System.Drawing.Size(75, 23);
            this.buttonChangeChannel.TabIndex = 0;
            // 
            // toolStripMenu
            // 
            this.toolStripMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuItemChangeChannel});
            this.toolStripMenu.Location = new System.Drawing.Point(0, 0);
            this.toolStripMenu.Name = "toolStripMenu";
            this.toolStripMenu.Size = new System.Drawing.Size(296, 25);
            this.toolStripMenu.TabIndex = 0;
            // 
            // menuItemChangeChannel
            // 
            this.menuItemChangeChannel.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.menuItemChangeChannel.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.menuItemChangeChannel.Name = "menuItemChangeChannel";
            this.menuItemChangeChannel.Size = new System.Drawing.Size(90, 22);
            this.menuItemChangeChannel.Text = "Change Channel";
            this.menuItemChangeChannel.Click += new System.EventHandler(this.menuItemChangeChannel_Click);
            // 
            // ChatChannel
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(296, 262);
            this.Controls.Add(this.textBoxLog);
            this.Controls.Add(this.toolStripMenu);
            this.Controls.Add(this.panelChatBox);
            this.DoubleBuffered = true;
            this.Name = "ChatChannel";
            this.Text = "Chat Channel";
            this.panelChatBox.ResumeLayout(false);
            this.panelChatBox.PerformLayout();
            this.toolStripCustomChat.ResumeLayout(false);
            this.toolStripCustomChat.PerformLayout();
            this.toolStripMenu.ResumeLayout(false);
            this.toolStripMenu.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        private RichTextBox textBoxLog;
        private Panel panelChatBox;
        private ToolStrip toolStripCustomChat;
        private ToolStripDropDownButton menuEmoticons;
        private ToolStripMenuItem menuItemEmoticonHappy;
        private ToolStripMenuItem menuItemEmoticonSad;
        private TextBox textBoxMsg;
        private Button buttonChangeChannel;
        private ToolStrip toolStripMenu;
        private ToolStripButton menuItemChangeChannel;


        private string _channelName;
        private string _tempLog;

        public string ChannelName
        {
            get
            {
                return this._channelName;
            }
            set
            {
                if ( value == null )
                {
                    value = string.Empty;
                }
                this._channelName = value.Trim();
            }
        }

        public ChatChannel()
        {
            this.InitializeComponent();

            this.menuItemEmoticonHappy.Click += this.emoticonClicked;
            this.menuItemEmoticonHappy.Tag = ":)";
            this.menuItemEmoticonSad.Click += this.emoticonClicked;
            this.menuItemEmoticonSad.Tag = ":(";

            this.textBoxMsg.KeyUp += this.textBoxMsg_KeyUp;

            this._channelName = "main";

            this.FormClosing += Chatchannel_FormClosing;
        }

        private void textBoxMsg_KeyUp(object sender, KeyEventArgs e)
        {
            if ( e.KeyCode == Keys.Enter )
            {
                string msg = textBoxMsg.Text.Trim();
                if ( !string.IsNullOrEmpty( msg ) )
                {
                    textBoxMsg.Clear();

                    Command cmd = new Command();
                    cmd.Intent = 30; //Send Channel Message
                    cmd.Add( "type", "text/plain" );
                    cmd.Add( "channel", this._channelName );
                    cmd.ExtraData = Command.CurrentEncoding.GetBytes( msg );
                    Program.openedForm.LINKED_SendData( cmd );
                }
            }
        }

        public void AddText(string name, string message)
        {
            if ( !string.IsNullOrEmpty( message ) )
            {
                message = message.Replace( @"\", @"\\" );
                message = message.Replace( @"{", @"\{" );
                message = message.Replace( @"}", @"\}" );
            }
            if ( !string.IsNullOrEmpty( name ) )
            {
                name = name.Replace( @"\", @"\\" );
                name = name.Replace( @"{", @"\{" );
                name = name.Replace( @"}", @"\}" );
                this._tempLog += @"\pard\par\b " + name + @": \b0 " + message;
            }
            else
            {
                this._tempLog += @"\pard\par " + message;
            }
            this.SetRepaint( false );
            this.textBoxLog.Rtf = @"{\rtf1 " + this._tempLog;
            this.textBoxLog.Select( this.textBoxLog.Text.Length, 0 );
            this.SetRepaint( true );
        }

        private void emoticonClicked(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem) sender;
            if ( item != null )
            {
                string itemTag = (string) item.Tag;
                if ( itemTag != null )
                {
                    textBoxMsg.AppendText( itemTag );
                }
            }
        }

        private void Chatchannel_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
        private void menuItemChangeChannel_Click(object sender, EventArgs e)
        {
            string newName = Input.GetText( "Change Channel", "Type the name of the channel." );
            if ( newName != null )
            {
                if ( string.IsNullOrEmpty( newName.Trim() ) )
                {
                    MessageBox.Show( "You must type in a channel name!", "Change Channel", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0 );
                }
                else
                {
                    this._channelName = newName;
                    this.Text = "Chat Channel - " + newName;
                }
            }
        }


        private IntPtr eventMask;
        private void SetRepaint(bool allow)
        {
            if ( allow )
            {
                NativeMethods.SendMessage( this.textBoxLog.Handle, 1093, IntPtr.Zero, eventMask ); //Turn on events.
                NativeMethods.SendMessage( this.textBoxLog.Handle, 11, new IntPtr( 1 ), IntPtr.Zero ); //Turn on redrawing.
                this.Invalidate( true );
            }
            else
            {
                NativeMethods.SendMessage( this.textBoxLog.Handle, 11, IntPtr.Zero, IntPtr.Zero ); //Stop redrawing.
                eventMask = NativeMethods.SendMessage( this.textBoxLog.Handle, 1083, IntPtr.Zero, IntPtr.Zero ); //Stop sending of events.
            }
        }

    }
}
